package com.cskaoyan.mall.dao;

import com.cskaoyan.mall.model.Goods;
import com.cskaoyan.mall.model.GoodsParam;
import com.cskaoyan.mall.model.Spec;
import com.cskaoyan.mall.model.Type;
import com.cskaoyan.mall.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.math.BigInteger;
import java.sql.SQLException;
import java.util.List;

/**
 * @PackgeName: com.cskaoyan.mall.dao
 * @ClassName: GoodsDaoImpl
 * @Author: admin
 * Date: 2020/5/3 21:06
 * project name: Javaproject1
 * @Version:
 * @Description:
 */
public class GoodsDaoImpl implements GoodsDao {
    @Override
    public List<Type> getType() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Type> typeList = null;
        try {
            typeList = runner.query("select * from type", new BeanListHandler<>(Type.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return typeList;
    }

    @Override
    public List<Goods> getGoodsByType(String typeId) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        List<Goods> goodsList = null;
        try {
            goodsList = runner.query("select * from goods where typeId = ?",
                    new BeanListHandler<>(Goods.class),
                    typeId);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return goodsList;
    }

    @Override
    public int addGoods(GoodsParam goodsParam) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into goods values (null,?,?,?,?,?,?)",
                    goodsParam.getImg(),
                    goodsParam.getPrice(),
                    goodsParam.getTypeId(),
                    goodsParam.getName(),
                    goodsParam.getStockNum(),
                    goodsParam.getDesc());
            //插入完成后，goods会有id。将此id赋值给spec表的goodsId
            return insertId();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }


    @Override
    public void addSpecList(int goodsId, List<Spec> specList) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        for (Spec spec : specList) {
            try {
                runner.update("insert into spec values (null,?,?,?,?)",
                        spec.getSpecName(),
                        spec.getStockNum(),
                        spec.getUnitPrice(),
                        goodsId);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *
     * @return  返回最近一次插入语句的主键
     */
    public int insertId() {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        BigInteger number = null;
        try {
            number = runner.query("select last_insert_id()", new ScalarHandler<>());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return number.intValue();
    }
}
